/** Scaled & spinning object derived from Object
**
** @version: $Id: SpinningObject.cpp 127 2010-12-04 21:25:26Z rujinxero $
*/

#include "SpinningObject.h"


/******************************************************************************\
*                           Spinning Object class                              *
\******************************************************************************/


/*****************************************************\
* Spinning Object constructor                         *
\*****************************************************/
SpinningObject::SpinningObject(const D3DXVECTOR3& cvPosition)
  : Object(cvPosition)
  , m_fAngle(0)
  , m_bSpinning(true)
  , m_fAngularVelocity(1.0f)
{
	/* Object's constructor calls UpdateWorldTransform */ 
}


/*****************************************************\
* Destructor                                          *
\*****************************************************/
SpinningObject::~SpinningObject()
{
}


/*****************************************************\
* How fast and in which direction does it spin?       *
\*****************************************************/
void SpinningObject::SetSpinRate(const float fSpinRate)
{
	m_fAngularVelocity = fSpinRate;
}


/*****************************************************\
* How fast and in which direction does it spin?       *
\*****************************************************/
void SpinningObject::SetSpinning(const bool bSpinning)
{
	m_bSpinning = bSpinning;
}


/*****************************************************\
* Update each frame                                   *
\*****************************************************/
bool SpinningObject::Update(const double cdDeltaTime)
{
	bool bShouldDraw = Object::Update(cdDeltaTime);

	if (m_bSpinning)
	{
		/* For updating this object, just rotate a little more */
		m_fAngle += (float)((double)m_fAngularVelocity * cdDeltaTime);
		UpdateWorldTransform();
	}

	return bShouldDraw;
}


/*****************************************************\
* Update world transform                              *
\*****************************************************/
void SpinningObject::UpdateWorldTransform()
{
	/*
	** Scale then rotate then translate
	*/

	/* Object::UpdateWorldTransform doesn't help here: we need to add rotation
	** between scaling and translating
	*/

	/* Scale in all three dimensions */
	D3DXMATRIX mScale;
	D3DXMatrixScaling(&mScale, m_fScale, m_fScale, m_fScale);

	/* Rotate around Z axis */
	D3DXMATRIX mRotate;
	D3DXMatrixRotationZ(&mRotate, m_fAngle);

	/* Translate to our location in space */
	D3DXMATRIX mTranslation;
	D3DXMatrixTranslation(&mTranslation, m_vPosition.x, m_vPosition.y, m_vPosition.z);

	/* Combine them into world matrix for use by renderer */
	D3DXMatrixMultiply(&m_mWorld, &mScale, &mRotate);			// scale then rotate
	D3DXMatrixMultiply(&m_mWorld, &m_mWorld, &mTranslation);	// then translate
}
